package com.chinairi.mall.service.impl;

import com.chinairi.mall.model.SupplierOrderProduceInformation;
import com.chinairi.mall.dao.SupplierOrderProduceInformationMapper;
import com.chinairi.mall.service.ISupplierOrderProduceInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * <p>
 * 供应商订单生产信息跟踪业务逻辑层接口实现类
 * </p>
 *
 * @author 李昌赐
 * @since 2020-12-11
 */
@Service
public class SupplierOrderProduceInformationServiceImpl extends ServiceImpl<SupplierOrderProduceInformationMapper, SupplierOrderProduceInformation> implements ISupplierOrderProduceInformationService {

    @Autowired
    private SupplierOrderProduceInformationMapper supplierOrderProduceInformationMapper;

    private static final Logger LOGGER = LoggerFactory.getLogger(ISupplierOrderProduceInformationService.class);

    @Override
    public Map<String, Object> listSupplierOrderProduceInformation(SupplierOrderProduceInformation supplierOrderProduceInformation) {
        if (StringUtils.isEmpty(supplierOrderProduceInformation.getOrderNo()) || StringUtils.isEmpty(supplierOrderProduceInformation.getMatCode())) {
            LOGGER.info("数据为空");
            return new HashMap<>(1);
        }
        Map<String, Object> map = supplierOrderProduceInformationMapper.getSupplierOrderProduceInformation(supplierOrderProduceInformation);
        if (map == null || map.keySet() == null || map.keySet().size() <= 0) {
            LOGGER.info("数据为空");
            return new HashMap<>(1);
        }
        List<Map<String, Object>> listProProductPlan = supplierOrderProduceInformationMapper.listProProductPlan(supplierOrderProduceInformation);
        if (listProProductPlan != null && !listProProductPlan.isEmpty()) {
            for (Map<String, Object> proProductPlan : listProProductPlan) {
                Map<String, Object> proInventory = supplierOrderProduceInformationMapper.getProInventory(proProductPlan.get("proProductPlanCode").toString());
                if (proInventory != null && !proInventory.isEmpty()) {
                    proProductPlan.put("actualAssemblyDateString", proInventory.get("actualAssemblyDateString"));
                    proProductPlan.put("planAssemblyDateString", proInventory.get("planAssemblyDateString"));
                    proProductPlan.put("inventoryQty",proInventory.get("inventoryQty"));
                }
                supplierOrderProduceInformation.setProProductPlanCode(proProductPlan.get("proProductPlanCode").toString());
                Map<String, Object> qltAssenblyInspection = supplierOrderProduceInformationMapper.listQltAssenblyInspection(supplierOrderProduceInformation);
                if (qltAssenblyInspection != null && !qltAssenblyInspection.isEmpty()) {
                    proProductPlan.put("inspectionDateString", qltAssenblyInspection.get("inspectionDateString"));
                    proProductPlan.put("warehouseInspectionQty", qltAssenblyInspection.get("warehouseInspectionQty"));
                    proProductPlan.put("qualifiedQty", qltAssenblyInspection.get("qualifiedQty"));
                }
                Map<String, Object> productIntoStorage = supplierOrderProduceInformationMapper.listProductIntoStorage(supplierOrderProduceInformation);
                if (productIntoStorage != null && !productIntoStorage.isEmpty()) {
                    proProductPlan.put("planStorageDateString", productIntoStorage.get("planStorageDateString"));
                    proProductPlan.put("inspectionDateString", productIntoStorage.get("inspectionDateString"));
                    proProductPlan.put("qualifiedQty", productIntoStorage.get("qualifiedQty"));
                }

            }
        }
        map.put("listProProductPlan", listProProductPlan);
        Map<String, Object> productOutStorage = supplierOrderProduceInformationMapper.listProductOutStorage(supplierOrderProduceInformation);
        if (productOutStorage != null && !productOutStorage.isEmpty()) {
            map.put("planDeliveryDateString", productOutStorage.get("planDeliveryDateString"));
            map.put("actualStorageDateString", productOutStorage.get("actualStorageDateString"));
            map.put("outStockFinishQty", productOutStorage.get("outStockFinishQty"));
        }
        return map;
    }

    private List<Map<String, Object>> listProInventory(List<Map<String, Object>> listStartProduce) {
        if (listStartProduce == null || listStartProduce.isEmpty()) {
            return new ArrayList<>(1);
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (Map<String, Object> map : listStartProduce) {
            if (map.get("proProductPlanCode") == null) {
                LOGGER.info("计划编号为：" + map.get("proProductPlanCode") + "没有开始生产");
                continue;
            }
            String proProductPlanCode = map.get("proProductPlanCode").toString();
            Map<String, Object> proInventory = supplierOrderProduceInformationMapper.getProInventory(proProductPlanCode);
            if (proInventory == null) {
                proInventory.put("proProductPlanCode", proProductPlanCode);
            }
            list.add(proInventory);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> listOutStorage(SupplierOrderProduceInformation supplierOrderProduceInformation) {
        return supplierOrderProduceInformationMapper.listOutStorage(supplierOrderProduceInformation);
    }

    @Override
    public Map<String, Object> getOrderInformation(String orderId, String goodsDbId) {
        if (StringUtils.isEmpty(orderId) || StringUtils.isEmpty(goodsDbId)) {
            return new HashMap<>(1);
        }
        Map<String, Object> projectPlanReq = supplierOrderProduceInformationMapper.getProjectPlanReq(orderId);
        Map<String, Object> orderInfo = supplierOrderProduceInformationMapper.getOrderInfo(orderId, goodsDbId);
        Map<String, Object> orderConfirm = new HashMap<>(6);
        orderConfirm.put("orderTimeString", orderInfo.get("orderTimeString"));
        orderConfirm.put("ogGoodsNumber", orderInfo.get("ogGoodsNumber"));
        orderConfirm.put("ogGoodsConfirmNumber", orderInfo.get("ogGoodsNumber"));
        orderConfirm.put("ogDeliveryDateString", orderInfo.get("ogDeliveryDateString"));
        orderConfirm.put("orderConfirmDateString", orderInfo.get("orderConfirmDateString"));
        orderConfirm.put("orderId", orderId);
        orderConfirm.put("circleColor", orderInfo.get("circleColor"));
        SupplierOrderProduceInformation supplierOrderProduceInformation = new SupplierOrderProduceInformation();
        supplierOrderProduceInformation.setOrderNo(orderId);
        supplierOrderProduceInformation.setMatCode(goodsDbId);
        List<Map<String, Object>> listProProductPlan = supplierOrderProduceInformationMapper.listProProductPlan(supplierOrderProduceInformation);
        Map<String, Object> productColorMap = new HashMap<>(2);
        productColorMap.put("orderId", orderId);
        if (listProProductPlan == null || listProProductPlan.isEmpty()) {
            productColorMap.put("productColor", "#e2e2e2");
        } else {
            productColorMap.put("productColor", "#37c936");
        }
        Map<String, Object> map = new HashMap<>(4);
        map.put("projectPlanReq", projectPlanReq);
        map.put("orderInfo", orderInfo);
        map.put("orderConfirm", orderConfirm);
        map.put("productColorMap", productColorMap);
        return map;
    }
}
